<template>
	<div class="app-container">
		<!-- 高级查询 -->
		<el-form v-show="searchVisible" ref="queryForm" :model="queryParams" :inline="true">

			<el-form-item label="区域名称:">
				<el-input v-model="queryParams.warehouseName" size="small" placeholder="请输入" clearable
					@keyup.enter.native="handleQuery" />
			</el-form-item>

			<!--      <el-form-item label="仓库电话:">-->
			<!--        <el-input v-model="queryParams.warehousePhone" size="small" placeholder="请输入" clearable @keyup.enter.native="handleQuery" />-->
			<!--      </el-form-item>-->


			<el-form-item>
				<el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">查询</el-button>
				<el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
			</el-form-item>
		</el-form>

		<!-- 工具栏 -->
		<el-row :gutter="10" class="mb8">
			<el-col :span="1.5">
				<el-button v-hasPermission="['web:tWhWarehouse:insert']" type="primary" icon="el-icon-plus" size="mini"
					@click="handleAdd">新增</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button v-hasPermission="['web:tWhWarehouse:update']" type="success" icon="el-icon-edit" size="mini"
					:disabled="isSingle" @click="handleEdit">修改</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button v-hasPermission="['web:tWhWarehouse:delete']" type="danger" icon="el-icon-delete" size="mini"
					:disabled="isMultiple" @click="handleRemove">删除</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button v-hasPermission="['web:tWhWarehouse:export']" type="warning" icon="el-icon-download"
					size="mini" @click="handleExport">导出</el-button>
			</el-col>
			<right-toolbar :show-search.sync="searchVisible" @queryTable="handleQuery" />
		</el-row>

		<!-- 数据列表 -->
		<el-table v-loading="loading" :data="page.records" highlight-current-row
			@selection-change="handleSelectionChange">
			<el-table-column type="selection" width="55" align="center" />
			<el-table-column label="序号" align="center" type="index" />
			<el-table-column label="区域名称" align="center" width="200" prop="warehouseName" />
			<!-- <el-table-column label="库架编号" align="center" width="200"/> -->
			<!--      <el-table-column label="管理员" align="center" prop="manager" />-->
			<!--      <el-table-column label="电话" align="center" prop="warehousePhone" />-->

			<!--      <el-table-column label="最大仓储量" align="center" prop="maxCapacity" />-->
			<!--      <el-table-column label="付款方式" align="center" prop="paymentTerm" />-->
			<!--  <el-table-column label="" align="center" prop="locationArea" /> -->
			<!--      <el-table-column label="创建时间" align="center" prop="createDate" width="180">-->
			<!--        <template slot-scope="scope">-->
			<!--          <span>{{ parseTime(scope.row.createDate, '{y}-{m}-{d}') }}</span>-->
			<!--        </template>-->
			<!--      </el-table-column>-->
			<!--      <el-table-column label="更新时间" align="center" prop="updateDate" width="180">-->
			<!--        <template slot-scope="scope">-->
			<!--          <span>{{ parseTime(scope.row.updateDate, '{y}-{m}-{d}') }}</span>-->
			<!--        </template>-->
			<!--      </el-table-column>-->
			<el-table-column label="操作" align="center" >
				<template slot-scope="scope">
					<el-button v-hasPermission="['web:tWhWarehouse:update']" size="mini" type="text" icon="el-icon-edit"
						@click="handleEdit(scope.row)">修改</el-button>
					<el-button v-hasPermission="['web:tWhWarehouse:delete']" size="mini" type="text"
						icon="el-icon-delete" @click="handleRemove(scope.row)">删除</el-button>
				</template>
			</el-table-column>
		</el-table>

		<!-- 分页 -->
		<pagination v-show="page.total>0" :total="page.total" :page.sync="page.current" :limit.sync="page.pageSize"
			@pagination="handleQuery" />

		<!-- 添加/修改对话框 -->
		<el-dialog :title="tWhWarehouseFormDialog.title" :visible.sync="tWhWarehouseFormDialog.visible"
			:close-on-click-modal="false" width="500px" append-to-body>
			<el-form ref="tWhWarehouseForm" :model="tWhWarehouseFormData" :rules="tWhWarehouseFormRules"
				label-width="80px">
				<!-- <el-form-item label="" prop="warehouseCode">
					<el-input v-model="tWhWarehouseFormData.warehouseCode" placeholder="请输入" />
				</el-form-item> -->
				<el-form-item label="区域名称" prop="warehouseName">
					<el-input v-model="tWhWarehouseFormData.warehouseName" placeholder="请输入区域名称" />
				</el-form-item>
			<!-- 	<el-form-item label="" prop="address">
					<el-input v-model="tWhWarehouseFormData.address" placeholder="请输入" />
				</el-form-item>
				<el-form-item label="" prop="manager">
					<el-input v-model="tWhWarehouseFormData.manager" placeholder="请输入" />
				</el-form-item>
				<el-form-item label="" prop="warehousePhone">
					<el-input v-model="tWhWarehouseFormData.warehousePhone" placeholder="请输入" />
				</el-form-item>
				<el-form-item label="" prop="version">
					<el-input v-model="tWhWarehouseFormData.version" placeholder="请输入" />
				</el-form-item>
				<el-form-item label="" prop="maxCapacity">
					<el-input v-model="tWhWarehouseFormData.maxCapacity" placeholder="请输入" />
				</el-form-item>
				<el-form-item label="" prop="paymentTerm">
					<el-input v-model="tWhWarehouseFormData.paymentTerm" placeholder="请输入" />
				</el-form-item>
				<el-form-item label="" prop="locationArea">
					<el-input v-model="tWhWarehouseFormData.locationArea" placeholder="请输入" />
				</el-form-item>
				<el-form-item label="备注" prop="remark">
					<el-input v-model="tWhWarehouseFormData.remark" placeholder="请输入备注" />
				</el-form-item>
				<el-form-item label="创建时间" prop="createDate">
					<el-date-picker v-model="tWhWarehouseFormData.createDate" type="date" value-format="yyyy-MM-dd"
						size="small" style="width: 200px" placeholder="选择创建时间" clearable />
				</el-form-item>
				<el-form-item label="更新时间" prop="updateDate">
					<el-date-picker v-model="tWhWarehouseFormData.updateDate" type="date" value-format="yyyy-MM-dd"
						size="small" style="width: 200px" placeholder="选择更新时间" clearable />
				</el-form-item> -->
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button type="primary" @click="confirmTWhWarehouseFormDialog">确 定</el-button>
				<el-button @click="resetTWhWarehouseFormDialog">取 消</el-button>
			</div>
		</el-dialog>
	</div>
</template>

<script>
	import TWhWarehouseApi from '@/api/web/TWhWarehouseApi'

	const defaultTWhWarehouseFormData = {
		warehouseCode: null,
		warehouseName: null,
		warehouseType: null,
		address: null,
		manager: null,
		warehousePhone: null,
		version: null,
		maxCapacity: null,
		paymentTerm: null,
		locationArea: null,
		createDate: null,
		updateDate: null
	}
	export default {
		name: 'TWhWarehouseList',
		data() {
			return {
				options: {
					sysYnTypeOptions: this.$constant.sysYnTypeOptions
				},
				queryParams: {
					warehouseCode: null,
					warehouseName: null,
					warehouseType: null,
					address: null,
					manager: null,
					warehousePhone: null,
					version: null,
					maxCapacity: null,
					paymentTerm: null,
					locationArea: null,
					createDate: null,
					updateDate: null,
					isValid: this.$constant.Y
				},
				page: {
					current: 1,
					pageSize: 10,
					total: 0,
					records: []
				},
				tWhWarehouseFormDialog: {
					title: '',
					visible: false
				},
				tWhWarehouseFormData: Object.assign({}, defaultTWhWarehouseFormData),
				tWhWarehouseFormRules: {
					warehouseName: [{
						required: true,
						message: '不能为空',
						trigger: 'blur'
					}],
					createDate: [{
						required: true,
						message: '创建时间不能为空',
						trigger: 'blur'
					}],
					updateDate: [{
						required: true,
						message: '更新时间不能为空',
						trigger: 'blur'
					}]
				},
				loading: true,
				searchVisible: true,
				multipleSelection: [],
				isSingle: true,
				isMultiple: true
			}
		},
		created() {
			this.handleQuery()
			this.loadOptions()
		},
		methods: {
			loadOptions() {},
			handleAdd() {
				this.openTWhWarehouseFormDialog()
			},
			handleRemove(row) {
				const ids = row.id || this.multipleSelection.map(item => item.id)
				this.$confirm('是否确认删除数据?', '提示', {
					confirmButtonText: '确定',
					cancelButtonText: '取消',
					type: 'warning'
				}).then(function() {
					return TWhWarehouseApi.remove(ids)
				}).then(() => {
					this.handleQuery()
					this.$message.success('删除成功')
				})
			},
			handleEdit(row) {
				const id = row.id || this.multipleSelection.map(item => item.id)[0]
				this.openTWhWarehouseFormDialog(id)
			},
			handleQuery() {
				this.loading = true
				TWhWarehouseApi.page(this.queryParams, this.page).then(response => {
					this.page.records = response.data.records
					this.page.total = response.data.total
					this.loading = false
				}, () => {
					this.loading = false
				})
			},
			handleExport() {
				this.$confirm('是否确认导出数据?', '提示', {
					confirmButtonText: '确定',
					cancelButtonText: '取消',
					type: 'warning'
				}).then(function() {
					return TWhWarehouseApi.exportData(this.queryParams)
				}).then(response => {
					this.$common.download(response.msg)
				})
			},
			openTWhWarehouseFormDialog(id) {
				this.resetTWhWarehouseFormDialog()
				this.tWhWarehouseFormDialog.visible = true
				if (id) {
					this.tWhWarehouseFormDialog.title = '仓库-修改'
					TWhWarehouseApi.get(id).then(response => {
						this.tWhWarehouseFormData = {
							...this.tWhWarehouseFormData,
							...response.data
						}
					})
				} else {
					this.tWhWarehouseFormDialog.title = '仓库-添加'
				}
			},
			confirmTWhWarehouseFormDialog() {
				this.$refs['tWhWarehouseForm'].validate(valid => {
					if (valid) {
						if (this.tWhWarehouseFormData.id) {
							TWhWarehouseApi.edit(this.tWhWarehouseFormData).then(response => {
								this.$message.success('修改成功')
								this.tWhWarehouseFormDialog.visible = false
								this.handleQuery()
							})
						} else {
							TWhWarehouseApi.add(this.tWhWarehouseFormData).then(response => {
								this.$message.success('添加成功')
								this.tWhWarehouseFormDialog.visible = false
								this.handleQuery()
							})
						}
					}
				})
			},
			resetTWhWarehouseFormDialog() {
				this.tWhWarehouseFormDialog = {
					title: '',
					visible: false
				}
				this.tWhWarehouseFormData = Object.assign({}, defaultTWhWarehouseFormData)
				this.resetForm('tWhWarehouseForm')
			},
			handleSelectionChange(selection) {
				this.multipleSelection = selection
				this.isSingle = selection.length !== 1
				this.isMultiple = !selection.length
			},
			resetQuery() {
				this.page.current = 1
				this.resetForm('queryForm')
				this.handleQuery()
			}
		}
	}
</script>
